﻿using System;
using System.Linq;
using System.Linq.Expressions;
using Medora.Core;
using Medora.DataContract;
using Medora.Model.Institutions;
using Medora.Services.Utils;
using Translator = Medora.Services.Basic.Translator;

namespace Medora.Services.Institutions
{
    public class ScheduleTemplatesService : ServiceBase<ScheduleTemplate, ScheduleTemplateFilter>, IScheduleTemplatesService
    {
        public ScheduleTemplatesService(IUow uow, IUserService userService, IFilterConstructor<ScheduleTemplateFilter, ScheduleTemplate> filterConstructor)
            : base(uow, userService, filterConstructor)
        {
            _repository = _uow.ScheduleTemplates;
        }

        public bool Save(Guid userId, ScheduleTemplate obj)
        {
            //todo add logging on save
            try
            {
                if (obj.Id != Guid.Empty)
                {
                    _uow.ScheduleTemplateItems.DeleteWhere(o => o.ScheduleTemplateId == obj.Id);
                    _uow.Commit();

                    _repository.Update(obj);
                    /*List<Guid> ids = new List<Guid>();
                    if (obj.Items != null)
                    {
                        ids = obj.Items.Select(o => o.Id).ToList();
                    }*/

                    //_uow.ScheduleTemplateItems.DeleteWhere(o => o.ScheduleTemplateId == obj.Id && !ids.Contains(o.Id));

                }
                else
                {
                    obj.CreatedBy = userId;
                    obj.CreatedOn = DateTime.Now;

                    _repository.Add(obj);
                }

                if (obj.Items != null)
                {
                    var items = obj.Items.Where(o => o.ScheduleTypeId != Guid.Empty).ToList();
                    if (items.Any())
                    {
                        foreach (var scheduleTemplateItem in items)
                        {
                            scheduleTemplateItem.CreatedOn = DateTime.Now;
                            scheduleTemplateItem.CreatedBy = userId;
                            _uow.ScheduleTemplateItems.Add(scheduleTemplateItem);
                        }
                    }    
                }
                

                _uow.Commit();
                return true;
            }
            catch (Exception e)
            {
                Logger.WriteLog(e);
                throw new Exception(Translator.Instance.Translate("Error during data save"));
            }
        }

        protected override Expression<Func<ScheduleTemplate, bool>> ConstructFilter(Expression<Func<ScheduleTemplate, bool>> startE, ScheduleTemplateFilter filter)
        {
            if (filter == null) return startE;

            var finalE = startE;

            if (filter.ProviderId!=Guid.Empty)
            {
                Expression<Func<ScheduleTemplate, bool>> fExpression =
                    o => o.ProviderId==filter.ProviderId;
                finalE = _filterConstructor.And(finalE, fExpression);
            }
            return finalE;


        }
    }

    public interface IScheduleTemplatesService : IService<ScheduleTemplate, ScheduleTemplateFilter>
    {
        
    }
}